﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using CallCenter.BLL;
using CallCenter.Model;

namespace CallCenter
{
    public partial class FormScript : Form
    {
        public FormScript()
        {
            InitializeComponent();
        }

        private void FormScript_Load(object sender, EventArgs e)
        {
            ScriptBLL bll = new ScriptBLL();
            ScriptsModel[] scripts = bll.GetByParentId(0);
            foreach (ScriptsModel script in scripts)
            {
                TreeNode treeNode = new TreeNode();
                treeNode.Text = script.Name;
                treeNode.Tag = script;
                tvScript.Nodes.Add(treeNode);
                FillNodes(treeNode, (int)script.Id);
            }
        }

        /// <summary>
        /// 把以parentId为父节点的节点都添加到PraentNode中去
        /// </summary>
        /// <param name="PraentNode"></param>
        /// <param name="parentId"></param>
        private void FillNodes(TreeNode PraentNode,int parentId)
        {
            ScriptBLL bll = new ScriptBLL();
            ScriptsModel[] scripts = bll.GetByParentId(parentId);
            foreach (ScriptsModel script in scripts)
            {
                TreeNode node = new TreeNode();
                node.Text = script.Name;
                node.Tag = script;
                PraentNode.Nodes.Add(node);
                FillNodes(node, (int)script.Id);
            }
        }

        private void tvScript_AfterSelect(object sender, TreeViewEventArgs e)
        {
            ScriptsModel model = (ScriptsModel)e.Node.Tag;
            txtBody.Text = model.Body;
            //MessageBox.Show(model.Id.ToString());
        }

        private void btnUpdateScript_Click(object sender, EventArgs e)
        {
            ScriptsModel script = (ScriptsModel)tvScript.SelectedNode.Tag;
            if (script == null)
            {
                MessageBox.Show("没选中节点你修改条毛哇!");
                return;
            }
            FormEditScript form = new FormEditScript();
            form.ScriptName = script.Name;
            form.ScriptBody = script.Body;
            if (form.ShowDialog() == DialogResult.OK)
            {
                script.Name = form.ScriptName;
                script.Body = form.ScriptBody;
                new ScriptBLL().UpdateById(script);
                tvScript.SelectedNode.Text = script.Name;
                txtBody.Text = script.Body;
            }
        }

        private void btnAddRootNode_Click(object sender, EventArgs e)
        {
            FormAddNode form = new FormAddNode();
            if (form.ShowDialog()==DialogResult.OK)
            {
                ScriptsModel model = new ScriptsModel();
                model.Name = form.scriptName;
                model.Body = form.scriptBody;
                model.ParentId = 0;
                model.Id = new ScriptBLL().AddNew(model);
                TreeNode node = new TreeNode();
                node.Text = model.Name;
                node.Tag = model;
                txtBody.Text = model.Body;
                tvScript.Nodes.Add(node);
            }
        }

        private void btnAddChildNode_Click(object sender, EventArgs e)
        {
            ScriptsModel selectdNode = (ScriptsModel)tvScript.SelectedNode.Tag;
            if (selectdNode == null)
            {
                MessageBox.Show("没选择节点");
                return;
            }
            FormAddNode form = new FormAddNode();
            if (form.ShowDialog() == DialogResult.OK)
            {
                ScriptsModel model = new ScriptsModel();
                model.Name = form.scriptName;
                model.Body = form.scriptBody;
                model.ParentId = selectdNode.Id;
                model.Id = new ScriptBLL().AddNew(model);
                TreeNode node = new TreeNode();
                node.Text = model.Name;
                node.Tag = model;
                txtBody.Text = model.Body;
                tvScript.SelectedNode.Nodes.Add(node);
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            ScriptsModel model = (ScriptsModel)tvScript.SelectedNode.Tag;
            new ScriptBLL().DeleteAllNodes((int)model.Id);
            tvScript.Nodes.Remove(tvScript.SelectedNode);
        }
    }
}
